--cb?
--1024791;164930;3028;-1;-1;-1;m.tratu.soha.vn;0;0;320x401;32;=%2F%3Ftitle%3Doutstanding%26dict%3Den_vn%26submit%3DTra%2Bt%25E1%25BB%25AB&re=http%3A//m.ngude.soha.vn/

local biz_name = "cb"

if test then
    ConfigRedisConnectors[biz_name]={
        host = test.host,
        port = test.port,
        queue = "queue_4"
    }
else
    ConfigRedisConnectors[biz_name]={
            host = "192.168.2.28",
            port = 6379,
            queue = "queue_4"
    }
end

local function cb()
return coroutine.create(function ()
-- constants
local REDIS_SEPERATOR = "\t"
local NUM_SEMICOLON = 11
local DEF_LOCATION = "0"
local cov = "1" -- click or view

local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
local hget_cmd_format = "hget "..ConfigRedisConnectors[config.HGET_MOBILE_REDIS].queue.." %s "
local mobile_cookie_domain = config.http.setcookie.mobile
local http_server_name = config.http.servername


-- headers
local local_config = config
local COOKIE_HEADER = local_config.COOKIE_HEADER
local USER_AGENT_HEADER = local_config.USER_AGENT_HEADER
local SET_COOKIE_HEADER = local_config.SET_COOKIE_HEADER
local REFERER_HEADER = local_config.REFERER_HEADER
local SERVER_HEADER = local_config.SERVER_HEADER
local http_server_name = local_config.http.servername
local PRAGMA_HEADER = local_config.PRAGMA_HEADER
local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
local EXPIRES_HEADER = local_config.EXPIRES_HEADER
local LAST_MODIFIED_HEADER = local_config.LAST_MODIFIED_HEADER
local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
local reply_ok = reply.status_type.ok
--cookies
local REGION_COOKIE = local_config.REGION_COOKIE
local GUID_COOKIE = local_config.GUID_COOKIE
local CREATE_COOKIE = local_config.CREATE_COOKIE
local EXPIRE_TIME = local_config.EXPIRE_TIME
local OS_MOBILE_COOKIE = local_config.OS_MOBILE_COOKIE
local REGION_COOKIE = local_config.REGION_COOKIE
local HGET_MOBILE_REDIS = local_config.HGET_MOBILE_REDIS
-- functions
local reply_pushHeader = reply.pushHeader
local reply_ok = reply.status_type.ok
local reply_setStatus = reply.setStatus
local request_getHeadersKV = request.getHeadersKV
local request_getParams = request.getParams
local request_getIP = request.getIP
local os_time = os.time
local to_number = tonumber
local util_redis_cmd = util.redis_cmd
local util_c_inet_network = util.c_inet_network
local util_parseKV = util.parseKV
local luredis_clear_buffer = luredis.clear_buffer
local luredis_concat_buffer = luredis.concat_buffer
local luredis_send_buffer = luredis.send_buffer
local luredis_send_cmd = luredis.send_cmd
local luredis_reply_string = luredis.reply_type.string
local url_decode = util.fast_url_decode
local l_pushCookieHeader = pushCookieHeader
local util_create_guid = util.create_guid
local l_parseCookies = parseCookies
local l_getMobileService = getMobileService
local coroutine_yield = coroutine.yield


while true do
    local paraTable
    local path
    while true do -- validate params
        paraTable = request_getParams(";",NUM_SEMICOLON-1);
        if #paraTable == 2 then
            local p2 = util.parse(paraTable[2],"&=")
            local re = p2[#p2]
            path = string.sub(paraTable[2],2,-string.len(re)-5)
            if re and string.byte(re,3) then
                --[[if string.find(re, "://") then
                re = re:gsub("%|","&")
                end]]
                reply_pushHeader(config.LOCATION_HEADER, url_decode(re))
                break;
            else
                reply_pushHeader(SERVER_HEADER, http_server_name)
                reply_pushHeader(CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
                reply_pushHeader(PRAGMA_HEADER, "no-cache")
                reply_pushHeader(EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:10 GMT")
                reply_pushHeader(CONTENT_TYPE_HEADER, "image/gif")
                reply_pushHeader(LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
                reply_pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
            end
        end
        --print("Invalid params")
        		reply_setStatus(302);
        coroutine_yield(); -- return due to incorrect number of params
    end

    local create_time = os_time();
    local expire_time = EXPIRE_TIME+create_time;
    local ip = request_getIP()

    local headerTable = request_getHeadersKV();
    local referer = headerTable[REFERER_HEADER];
    -- cookie
    local cookieString = headerTable[COOKIE_HEADER];
    local cookieTable = util_parseKV(cookieString,";=");
    local create_cookie = cookieTable[CREATE_COOKIE]

    -- guid
    local guid = cookieTable[GUID_COOKIE];
    if not guid then
        guid = util_create_guid(create_time, ip);
        if guid then
            create_cookie = create_time
            l_pushCookieHeader(GUID_COOKIE, guid, expire_time, "/", mobile_cookie_domain, false)
            l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", mobile_cookie_domain, false)
        else
            guid = "0"
        end
    end
    
    if not create_cookie then 
    	create_cookie = create_time
	    l_pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", mobile_cookie_domain, false)
    end
    -- get region
    local region = cookieTable[REGION_COOKIE];
    if not region then -- ask redis for region
        local ipnum = util_c_inet_network(ip)
        if ipnum then
            region = util_redis_cmd(HGET_MOBILE_REDIS,false,
                                luredis_send_cmd,hget_cmd_format,ipnum);
        end
        if type(region) ~= "string" then
            region = DEF_LOCATION
        else
            l_pushCookieHeader(REGION_COOKIE, region, expire_time, "/", mobile_cookie_domain, false)
        end
    end


    local os = cookieTable[OS_MOBILE_COOKIE]
    local os_expire_time = create_time+86400;
    if not os or not string.byte(os,3) then
        os = parseUserAgent(headerTable[USER_AGENT_HEADER])
        if os then
            l_pushCookieHeader(OS_MOBILE_COOKIE, os, os_expire_time, "/", mobile_cookie_domain, false)
        else
            os = ""
        end
    end

    --For resoving conflicts in Thanh's side
    os = os.."-1"
    ----------------
    luredis_clear_buffer()
    luredis_concat_buffer(REDIS_SEPERATOR,
        create_time,  ip,  cov,  guid,  create_cookie,  region,
        paraTable[1], url_decode(path), referer,  os)
    if util_redis_cmd(biz_name,true,luredis_send_buffer,lpush_cmd_format) then
		reply_setStatus(302);
    end
    coroutine_yield()
end
end)
end

creators[biz_name] = function()
        f[biz_name] = cb()
end